﻿define('basicObject',
    ['jquery', 'ko'],
function ($, ko) {
    var basicObject = function (columnDefs, funcNewSelectedObject, funcNewSearchFields, method, container, title) {
        var self = this;
        self.entities = ko.observableArray();
        self.selectedObj = ko.observable(new funcNewSelectedObject());
        self.columnDefs = ko.observable(columnDefs);
        self.searchFields = ko.observable(new funcNewSearchFields());
        self.selectedItems = ko.observableArray([]);
        self.container = ko.observable(container);
        self.title = ko.observable(title);
        self.method = method;
        
        
        self.loadNew = function () {
            self.selectedObj(new funcNewSelectedObject());
        };

        self.sortInfo = ko.observable();
        self.sortInfo.subscribe(function (data) {
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage(), self.sortInfo());
        });


        self.pagingOptions = {
            pageSizes: ko.observableArray([10, 15, 30]),
            pageSize: ko.observable(10),
            totalServerItems: ko.observable(0),
            currentPage: ko.observable(1)
        };

        self.fillData = function (data) {
            if (data != null) {
                self.entities(data.entities);
                self.pagingOptions.totalServerItems(data.count);
            }

        };

        this.getPagedDataAsync = function (pageSize, page, sortInfo) {
            var sort = "";
            if (sortInfo != null) {
                sort = "{ 'field': '" + sortInfo.column.field + "', 'direction': '" + sortInfo.direction + "' }";
            } else {
                sort = "{ 'field': '', 'direction': '' }";
            }

            var filter = ko.toJSON(self.searchFields, null, 2);
            SendAjax(self.method, { pageSize: pageSize, page: page, filter: filter, ss: sort }, self.fillData);
        };

        self.RefreshGrid = function () {
            self.selectedItems([]);
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
        };

        self.pagingOptions.pageSizes.subscribe(function (data) {
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
        });

        self.pagingOptions.pageSize.subscribe(function (data) {
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
        });
        self.pagingOptions.totalServerItems.subscribe(function (data) {
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
        });
        self.pagingOptions.currentPage.subscribe(function (data) {
            self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());
        });

        self.getPagedDataAsync(self.pagingOptions.pageSize(), self.pagingOptions.currentPage());

        self.search = function () {
            self.pagingOptions.currentPage(1);
            self.RefreshGrid();
        };

        self.clearSearch = function () {
            self.searchFields(new funcNewSearchFields());
            self.RefreshGrid();
        };

        self.isDoubleClick = function (oldValue, currentValue) {
            var responseTime = 400;

            if ((currentValue - oldValue) <= responseTime) {

                self.clickTime = currentValue;
                return true;
            }

            self.clickTime = currentValue;
            return false;
        };

        self.afterSelectionChange = function (rowItem, event) {

            if (event.type == "click" && self.isDoubleClick(self.clickTime, event.timeStamp)) {
                self.selectedObj().load(rowItem.entity);
                var dlg = new MedoraDialogWindow($("#"+self.container()));
                dlg.save = function () {
                    self.selectedObj().save(self, dlg);
                };
                var content = $("#formContainer").html();
                dlg.createWithSave(400, 300, self.title(), "<div id='dlg'>" + content + "</div>");
                ko.applyBindings(self.selectedObj(), $("#dlg")[0]);
                return false;
                
            }

        };

        self.gridOptions = {
            data: self.entities,
            enablePaging: true,
            pagingOptions: self.pagingOptions,
            selectedItems: self.selectedItems,
            columnDefs: self.columnDefs(),
            autogenerateColumns: false,
            showColumnMenu: false,
            showFilter: false,
            sortInfo: self.sortInfo,
            afterSelectionChange: self.afterSelectionChange
        };
    };

    return basicObject;

});